package a66weding.com.jiehuntong.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Scroller;


public class SwipteLayout2 extends FrameLayout {
    private LinearLayout countView;
    private Button menuView;
    private Scroller scroller;
    private int countWidth;
    private int countHeight;
    private int menuWidth;
    private int menuHeight;
    private OnItemStateChanged onItemStateChanged;
    private boolean isopen;
    private boolean isIntercept;
    public SwipteLayout2(Context context, AttributeSet attrs) {
        super(context, attrs);
        scroller = new Scroller(context);
    }
    public boolean isopen() {
        return isopen;
    }
    public void setIsopen(boolean isopen) {
        this.isopen = isopen;
    }
    //1.获取到两个子View对象:在Activity创建好所有视图对象后，会调用onFinishInflate（）方法
    //可获取子视图对象
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        countView = (LinearLayout) getChildAt(0);
        menuView = (Button) getChildAt(1);
    }
    //获取子视图对象的宽高：在onMeasure()方法中可获取
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        countWidth = countView.getMeasuredWidth();
        countHeight = countView.getMeasuredHeight();
        menuWidth = menuView.getMeasuredWidth();
        menuHeight = menuView.getMeasuredHeight();
    }
    //重写onLayout对两个子View进行布局
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        menuView.layout(countWidth, 0, countWidth + menuWidth, countHeight);
    }
    int lastX, lastY;
    int startX, startY;
    int totalX, totalY;
    //给Item添加触摸监听
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int eventX = (int) event.getRawX();
        int eventY = (int) event.getRawY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = eventX;
                lastY = eventY;
                startX = eventX;
                startY = eventY;
                break;
            case MotionEvent.ACTION_MOVE:
                int dx = eventX - lastX;
                int dy = eventY - lastY;
                int scrolltoX = getScrollX() - dx;
                //限定范围
                if (scrolltoX > menuWidth) {
                    scrolltoX = menuWidth;
                }
                if (scrolltoX < 0) {
                    scrolltoX = 0;
                }
                scrollTo(scrolltoX, 0);
                lastX = eventX;
                lastY = eventY;
                //事件反拦截条件
                totalX = Math.abs(eventX - startX);
                totalY = Math.abs(eventY - startY);
                if (totalX > totalY && totalX > 50) {
                    //实行反拦截
                    getParent().requestDisallowInterceptTouchEvent(true);
                    isIntercept = true;
                } else {
                    isIntercept = false;
                }
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                if (getScrollX() > menuWidth / 2) {
                    openMenu();
                } else {
                    closeMenu();
                }
                break;
        }
        return true;
    }

    //打开菜单
    public void openMenu() {
        scroller.startScroll(getScrollX(), getScrollY(), menuWidth - getScrollX(), 0);
        invalidate();
        this.setIsopen(true);
        if (onItemStateChanged != null) {
            onItemStateChanged.onOpen(this);
        }
    }

    //关闭菜单
    public void closeMenu() {
        scroller.startScroll(getScrollX(), getScrollY(), -getScrollX(), 0);
        invalidate();
        if (onItemStateChanged != null) {
            onItemStateChanged.onClose(this);
        }
    }

    //调用invalidate()时需要重写computeScroll()
    @Override
    public void computeScroll() {
        super.computeScroll();
        if (scroller.computeScrollOffset()) {
            scrollTo(scroller.getCurrX(), scroller.getCurrY());
            invalidate();
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        boolean isIntercept = false;
        int eventX = (int) event.getRawX();
        int eventY = (int) event.getRawY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = eventX;
                startY = eventY;
                if (onItemStateChanged != null) {
                    onItemStateChanged.onDown(this);
                }
                break;
            case MotionEvent.ACTION_MOVE:

                //事件拦截条件
                totalX = Math.abs(eventX - startX);
                totalY = Math.abs(eventY - startY);
                if (totalX > totalY && totalX > 132) {
//                    //实行拦截
                    isIntercept = true;
                    if (onItemStateChanged != null) {
                        onItemStateChanged.onDown(this);
                    }
                }
//
                break;

            case MotionEvent.ACTION_UP:
                break;
        }

        return isIntercept;

    }

    public void setOnItemStateChanged(OnItemStateChanged onItemStateChanged) {
        this.onItemStateChanged = onItemStateChanged;
    }


    public interface OnItemStateChanged {
        public void onOpen(SwipteLayout2 layout);//当item打开时

        public void onClose(SwipteLayout2 layout);//当item关闭时

        public void onDown(SwipteLayout2 layout);//当手指触摸某个item时
    }
}
